//
//  CustomMessageBaseCell.m
//  FFMall
//
//  Created by ale tan on 2017/9/26.
//  Copyright © 2017年 IT.Bear. All rights reserved.
//

#import "CustomMessageBaseCell.h"
#import <UIImageView+WebCache.h>
#import "Masonry.h"
#import "FFTools.h"

@implementation CustomMessageBaseCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        headerView = [UIImageView new];
        headerView.backgroundColor = [UIColor clearColor];
        headerView.userInteractionEnabled = YES;
        headerView.layer.masksToBounds = YES;
        headerView.layer.cornerRadius = HeaderWidth/2;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headBeenTaped:)];
        [headerView addGestureRecognizer:tap];
        
        UILongPressGestureRecognizer *headlongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(headBeenLongPress:)];
        [headerView addGestureRecognizer:headlongPress];
        
        headerView.image = [UIImage imageNamed:@"faceDefault"];
        [self.contentView addSubview:headerView];
        
        NSArray *IDs = [reuseIdentifier componentsSeparatedByString:kReuseIDSeparate];
        
        NSAssert(IDs.count>=2, @"reuseIdentifier should be separate by -");
        
        isReceived = [IDs[0] integerValue];
        
        if (!isReceived)//是我自己发送的
        {
            [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.contentView.mas_top).offset(TopEdge);
                make.width.mas_equalTo(HeaderWidth);
                make.height.mas_equalTo(HeaderHeight);
             make.right.mas_equalTo(self.contentView.mas_right).offset(-HeaderToEdge);
            }];
            
        }else//别人发送的消息
        {
            [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.contentView.mas_top).offset(TopEdge);
                make.width.mas_equalTo(HeaderWidth);
                make.height.mas_equalTo(HeaderHeight);
             make.left.mas_equalTo(self.contentView.mas_left).offset(HeaderToEdge);
            }];
            
        }
        
        bubbleImageView = [UIImageView new];
//        bubbleImageView.contentMode = UIViewContentModeScaleAspectFill;
        bubbleImageView.backgroundColor = [UIColor clearColor];
        bubbleImageView.userInteractionEnabled = YES;
        UILongPressGestureRecognizer *bubblelongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
        [bubbleImageView addGestureRecognizer:bubblelongPress];
        [self.contentView addSubview:bubbleImageView];
    }
    
    return self;
}

- (void)setBubbleImage{
    if (!isReceived)//是我自己发送的
    {
        bubbleImageView.image = [[UIImage imageNamed:kImageNameChat_send_nor] stretchableImageWithLeftCapWidth:10 topCapHeight:30];
        
//        bubbleImageView.highlightedImage = [[UIImage imageNamed:kImageNameChat_send_press] stretchableImageWithLeftCapWidth:10 topCapHeight:30];
        
    }else//别人发送的消息
    {
        bubbleImageView.image = [[UIImage imageNamed:kImageNameChat_Recieve_nor]stretchableImageWithLeftCapWidth:10 topCapHeight:30];
//        bubbleImageView.highlightedImage = [[UIImage imageNamed:kImageNameChat_Recieve_press] stretchableImageWithLeftCapWidth:10 topCapHeight:30];
    }
}

- (void)setModel:(JMUIChatModel *)model{
    _model = model;
    
    if (model.message.isReceived) {
        [headerView setImage:[UIImage imageNamed:@"fenkeCustomer"]];
    }
    else{
        [headerView setImage:[UIImage imageNamed:@"faceDefault"]];
    }
    
    JMSGCustomContent *content = (JMSGCustomContent *)model.message.content;
    NSDictionary *userinfo = content.customDictionary[@"user"];
    if ([userinfo isKindOfClass:[NSString class]]) {
        NSString *jsonStr = content.customDictionary[@"user"];
        userinfo = [FFTools toArrayOrNSDictionary:jsonStr];
    }
    
    if (userinfo[@"avatar"]) {
        [headerView sd_setImageWithURL:[NSURL URLWithString:userinfo[@"avatar"]] placeholderImage:nil];
    }
    
//    [model.message.fromUser thumbAvatarData:^(NSData *data, NSString *objectId, NSError *error) {
//        if (error == nil) {
//            JMSGUser *user = ((JMSGUser *)self.model.message.fromUser);
//            if ([objectId isEqualToString:user.username]) {
//                if (data != nil) {
//                    [headerView setImage:[UIImage imageWithData:data]];
//                } else {
//                    [headerView setImage:[UIImage imageNamed:@"faceDefault"]];
//                }
//            } else {
//                NSLog(@"该头像是异步乱序的头像");
//            }
//        } else {
//            NSLog(@"Action -- get thumbavatar fail");
//            [headerView setImage:[UIImage imageNamed:@"faceDefault"]];
//        }
//    }];
    
    [self setBubbleImage];
}

-(void)headBeenTaped:(UITapGestureRecognizer *)tap
{
    [self routerEventWithType:EventChatCellHeadTapedEvent userInfo:@{kModelKey:self.model}];
}

-(void)headBeenLongPress:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan)
    {
        [self routerEventWithType:EventChatCellHeadLongPressEvent userInfo:@{kModelKey:self.model}];
    }
    
}

-(void)longPress:(UILongPressGestureRecognizer *)Press
{
    
}

-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}

@end
